package hubol;

import java.awt.Point;
public class Mask {
	public double width, height;
	public Point originOffset;
	
	/**create a mask from a sprite*/
	public Mask(Sprite sprite){
		this(sprite.texture.width, sprite.texture.height, (int)(sprite.origin.dx * sprite.texture.width), (int)(sprite.origin.dy * sprite.texture.width));
	}
	
	/**create a mask from width, height, originX, and originY*/
	public Mask(int w, int h, int x, int y){
		width = w;
		height = h;
		originOffset = new Point(x, y);
	}
	
	public boolean collidesWith(Entity boy, double x1, double y1, Entity girl){
		if (!(boy.mask == null || girl.mask == null)){
			//Compute center of boy
			Point o1 = new Point(boy.mask.originOffset);
			o1.x *= -boy.xscale;
			o1.y *= -boy.yscale;
			x1 += o1.x;
			y1 += o1.y;
			x1 += boy.xscale * width / 2;
			y1 += boy.yscale * height / 2;
			//Compute center of girl
			double x2 = girl.x;
			double y2 = girl.y;
			
			Point o2 = new Point(girl.mask.originOffset);
			o2.x *= -girl.xscale;
			o2.y *= -girl.yscale;
			x2 += o2.x;
			y2 += o2.y;
			x2 += girl.xscale * girl.mask.width / 2;
			y2 += girl.yscale * girl.mask.height / 2;
			double dx, dy;
			dx = Math.abs(x2 - x1);
			dy = Math.abs(y2 - y1);
			return (dx < width / 2 * Math.abs(boy.xscale) + girl.mask.width / 2 * Math.abs(girl.xscale) && 
					dy < height / 2 * Math.abs(boy.yscale) + girl.mask.height / 2 * Math.abs(girl.yscale));
		}
		return false;
	}
}
